package com.example.demo.service.impl;

import com.example.demo.controller.requests.ModifyDrinkRequest;
import com.example.demo.service.IDrinkService;
import com.example.demo.model.Drink;
import com.example.demo.repository.DrinkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.server.ObjID;
import java.util.List;
import java.util.Optional;

@Service
public class DrinkJpaService implements IDrinkService {
    @Autowired
    private DrinkRepository drinkRepository;
    @Override
    public List<Drink> getAllDrinks() {
        return drinkRepository.findAll();
    }

    @Override
    public void addNewDrink(Drink drink) {
        drinkRepository.save(drink);
    }

    @Override
    public Drink getDrinkByID(Integer id) {
        return drinkRepository.findById(id).orElse(null);
    }

    @Override
    public void updataDrink(Drink drink) {
        drinkRepository.save(drink);//新增和修改都是save方法，判断逻辑，id为空save就是新增，否则就是修改
    }

    @Override
    public void deleteDrinkById(Integer id) {
        drinkRepository.delete(drinkRepository.getById(id));
    }


}
